/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxloadpopricefromdec.p                           */
/* PROCEDURE TYPE        : PO Price Load CIM-LOAD Process                   */
/* DESCRIPTION           : PO Price Load CIM-LOAD Process                   */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               19-Jun-19   */
/****************************************************************************/ 
/* CREATED BY            : Gaoyan                               22-Oct-20   */
/*ECO:GY01 ADD COST load process after successful import po price           */ 
/* CREATED BY            : Gaoyan                               28-Apr-21   */
/*ECO:GY02 update cost by price date                                        */ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}


&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)


define variable viReturn                     as integer     no-undo.
define variable ilReturnDataset              as logical     no-undo.
define variable ilPartialUpdate              as logical     no-undo.
define variable icPartialUpdateExceptionList as character   no-undo.
define variable ocPrimaryKey                 as character   no-undo.
define variable ocRowid                      as character   no-undo.
define variable oiDraftInstance              as integer     no-undo.
define variable ocPrimaryKeyName             as character   no-undo.
define variable vho                          as handle.
define variable oiReturnStatus               as integer     no-undo.

DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "YES"      no-undo.
define variable v_site                       like si_site   no-undo.
define variable R_import                     as logical     init   "No"       no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_status              as character                no-undo.
define variable v_existpod            as logical   initial no     no-undo.
define variable v_existpc             as logical   initial no     no-undo.
define variable v_overlap             as logical   initial no     no-undo.
define variable v_overlap1             as logical   initial no     no-undo.
define variable v_newpirce             as logical   initial no     no-undo.
/* below variable used by cim load result */
define variable v_err                as logical   initial no     no-undo.
define variable v_errstring           as character                no-undo.
define variable v_fline               as character                no-undo.
define buffer b_ptp_det for ptp_det.
define temp-table ttpcmstr 
    field pc_list like pc_mstr.pc_list
        field pc_desc like pc_mstr.pc_desc
        field pc_part like pc_mstr.pc_part
        field pc_domain like pc_mstr.pc_domain
        field pc_curr like pc_mstr.pc_curr
        field pc_amt_type like pc_mstr.pc_amt_type
        field pc_amt   like pc_mstr.pc_amt
        field pc_start like pc_mstr.pc_start
        field pc_expire like pc_mstr.pc_expire
        field pc_um like pc_mstr.pc_um
        field pc_temp_pl like pc_mstr.pc_temp_pl
        field pc_taskid as character
        field pc_supplier as character 
    field pc_error as character
  field pc_temp_remark as character   /* add by shirly 2020-03-03 */
    index tid pc_taskid
    index tid1 pc_domain pc_part pc_list.
        
/* GY01 ADD BEGIN */
define temp-table ttcost no-undo
           field tt_part       like  pt_mstr.pt_part
           field tt_site         like loc_mstr.loc_site   
           field tt_cost         like sct_det.sct_mtl_tl   
           field tt_pricelist    like pc_mstr.pc_list
           field tt_startdate    like pc_mstr.pc_start        
           field tt_taskid       as   character                   
           field tt_error        as   character
                   field tt_pm_code      as   character.


define variable   i               as integer no-undo.        
define variable   v_costerr       as logical no-undo.

/* GY01 ADD END */

function fiGetNextRowID returns decimal:
        lvi_row_id = lvi_row_id - 1.
        return lvi_row_id.
        end function. /* fiGetNextRowID */ 
                
                
form
   v_site                                  colon 25  skip
   v_import                                colon 25  skip
   r_import                                colon 25  skip
with frame a width 80 side-labels .
setframelabels(frame a:handle).

  
  clear frame a .
  

  
mainloop:
repeat:
  
   if c-application-mode <> 'web':u
   then do:
  
  siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE v_site     
             WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input v_site"}
            IF recno <> ? THEN DO:
               ASSIGN v_site = si_site.
               DISPLAY V_site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = v_site)
                                                                        
                                                                          
      THEN DO:
         /* Invalid Site */
         { us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT v_site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* Site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT v_site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT v_site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */  
   
          update
      v_import
          r_import
      with frame a.
   
   end.
   
   if (c-application-mode = "WEB":U)
   then do:
      {us/wb/wbrp06.i &command = update
      &fields = "v_site v_import r_import"
      &frm = "a"}
   end.
   
   if  (c-application-mode <> 'web') or  (c-application-mode = 'web' and (c-web-request begins 'data'))
   then do:
      bcdparm = "".
      {us/mf/mfquoter.i v_site}
      {us/mf/mfquoter.i v_import }
          {us/mf/mfquoter.i r_import }
   end.

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}
   m_workpath = m_workpath + "pc/".
   m_archpath = m_archpath + "pc/".
   m_outpath = m_outpath + "pc/".
   /* 
   {us/bbi/mfphead.i}
   */
   
   empty temp-table ttpcmstr.
   empty temp-table ttcost.  /* GY01 ADD */
   define buffer b_ttpcmstr for ttpcmstr.
   
   /* determine whether reload error data */
   if r_import then 
      v_status = "1,3".
   else
      v_status = "0".
  

   for each tea_task_sub no-lock where  tea_task_sub.domain = global_domain
                            and upper(tea_task_sub.Site)   = upper(v_site)
                                                        and tea_task_sub.subscriber = "QAD"
                                                        and tea_task_sub.tablename = "TES_PURCHASE_PRICE" 
                                                        and (tea_task_sub.TaskState = 0 or 
                                                        lookup(string(tea_task_sub.TaskState),v_status) > 0)
                                                             break by tea_task_sub.createtime desc
                                                        
                                                           .
        l_error = "".
     
         for each TES_PURCHASE_PRICE where TES_PURCHASE_PRICE.domain = global_domain
         /*
                             and TES_PURCHASE_PRICE.site   = tea_task_sub.Site
                                                 */
                                                 and TES_PURCHASE_PRICE.taskid = tea_task_sub.taskid
                                                 no-lock.
          
      status default TES_PURCHASE_PRICE.partcode.
        
          find   first ttpcmstr where        ttpcmstr.pc_part =   TES_PURCHASE_PRICE.PartCode
                                           and ttpcmstr.pc_domain =    TES_PURCHASE_PRICE.domain
                                           and ttpcmstr.pc_Curr =      TES_PURCHASE_PRICE.currency
                                           and ttpcmstr.pc_amt_type =  TES_PURCHASE_PRICE.AmountType                                                      
                                           and ttpcmstr.pc_start =    date(TES_PURCHASE_PRICE.start)
                               and ttpcmstr.pc_list =    TES_PURCHASE_PRICE.PriceList
                                           and ttpcmstr.pc_um      = TES_PURCHASE_PRICE.um         
                              /*                                                            
                                                           and ttpcmstr.pc_taskid  = TES_PURCHASE_PRICE.taskid
                                                           */
                                                           no-error .
                if available ttpcmstr then 
                l_error = "Duplicate PriceList".
         
         /*
     if not  available ttpcmstr then do:
     */
          create ttpcmstr.
          assign ttpcmstr.pc_list     =   TES_PURCHASE_PRICE.PriceList
                 ttpcmstr.pc_desc     =   ""
                         ttpcmstr.pc_part     =   TES_PURCHASE_PRICE.PartCode
                         ttpcmstr.pc_domain   =   TES_PURCHASE_PRICE.domain
                         ttpcmstr.pc_Curr     =   TES_PURCHASE_PRICE.currency
                         ttpcmstr.pc_amt_type =   TES_PURCHASE_PRICE.AmountType  
                         ttpcmstr.pc_start    =   date(TES_PURCHASE_PRICE.start)
             ttpcmstr.pc_expire   =   date(TES_PURCHASE_PRICE.ExpireDate)
                         ttpcmstr.pc_um       =   TES_PURCHASE_PRICE.um
             ttpcmstr.pc_amt[1]   =   TES_PURCHASE_PRICE.amount
                         ttpcmstr.pc_temp_pl  =   no                                                  
                         ttpcmstr.pc_taskid   =   TES_PURCHASE_PRICE.taskid                         
             ttpcmstr.pc_supplier =   upper(TES_PURCHASE_PRICE.SupplierCode)
                         ttpcmstr.pc_error    = l_error
                         ttpcmstr.pc_temp_remark  = if TES_PURCHASE_PRICE.Desc_   = "" then "no" else TES_PURCHASE_PRICE.Desc_  /*add by shirly 2021-03-03 temp-price */
                         .
   
   /*
   end.
   else do:   
        
        assign
                 ttpcmstr.pc_error  = "Duplicate".
                        
 
    end. /*else do: if available ttpcmstr , means duplicate record */
          */ 
        end. /* for each TES_PURCHASE_PRICE */
   end. /* for each tea_task_sub  */
  
   release tea_task_sub.
   
  if v_import then do:  
   /* update task status */
   for each ttpcmstr break by ttpcmstr.pc_taskid.
      if first-of(ttpcmstr.pc_taskid) then do:
          find first b_ttpcmstr where b_ttpcmstr.pc_taskid = ttpcmstr.pc_taskid 
                                          and b_ttpcmstr.pc_error <> ""
                                                                          no-error.
      find first tea_task_sub where tea_task_sub.taskid = ttpcmstr.pc_taskid
                              and tea_task_sub.tablename = "TES_PURCHASE_PRICE"
                                                  and tea_task_sub.subscriber = "QAD"
                          no-error.
          if available tea_task_sub then
          assign tea_task_sub.TaskState = if available b_ttpcmstr then 4 else 1
                 tea_task_sub.FailedInfo = if available b_ttpcmstr then b_ttpcmstr.pc_error else "QAD Read"
                 tea_task_sub.UpdateUser = global_userid
                         tea_task_sub.UpdateTime = datetime(today,mtime).
         end.
   end.
   
   
   

   /* when select import data */

   /* scenario 1 PC Master not exist for price list + part + um
      pc_start < start date and pc_start < expired date means overlap*/
   /* scenario 2 PC Master exist */
   /***
   /* Mark overlap data */
   for each ttpcmstr:
   
             /* overlap price need to undo */ 
    if not can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start >= ttpcmstr.pc_start 
                                         no-lock)
                        and 
         can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start <= ttpcmstr.pc_expire
                                         no-lock) then do:
                                         assign v_overlap = yes
                                         ttpcmstr.pc_error = "ERROR".
                                  
                                         end.
                                         else assign v_overlap = no.
                                         
         if can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start < ttpcmstr.pc_start 
                                         no-lock)
                        and 
         can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start <= ttpcmstr.pc_expire
                                         no-lock) then do:
                                         assign v_overlap1 = yes
                                         ttpcmstr.pc_error = "ERROR".
                                  
                                         end.
                                         else assign v_overlap1 = no.
                                         
         if v_overlap or v_overlap1 then 
          do:
          find first tea_task_sub where tea_task_sub.taskid = ttpcmstr.pc_taskid
                              and tea_task_sub.tablename = "TES_PURCHASE_PRICE"
                                                  and tea_task_sub.subscriber = "QAD"
                          no-error.
          if available tea_task_sub then
          assign tea_task_sub.TaskState = 3
                 tea_task_sub.FailedInfo = "Overlap error"
                 tea_task_sub.UpdateUser = global_userid
                         tea_task_sub.UpdateTime = today.
          end. 

        end. /* check overlap */
        ***/
   for each ttpcmstr where /* ttpcmstr.pc_error = "" */ break by ttpcmstr.pc_taskid:
      if first-of(ttpcmstr.pc_taskid) then do:
          if not can-find (first b_ttpcmstr where b_ttpcmstr.pc_taskid = ttpcmstr.pc_taskid 
                          and b_ttpcmstr.pc_error <> "") then do:
          
      v_cim = m_workpath + "SPC" + "_" + string(ttpcmstr.pc_taskid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".        
      v_log = m_workpath + "SPC" + "_" + string(ttpcmstr.pc_taskid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".                  
      
          output stream f_cim to value(v_cim).
     end.

        /* new price */                                 
    if not can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start < ttpcmstr.pc_start 
                                         and   pc_mstr.pc_list_classification  = 1 
                                         no-lock)
        and not can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start <= ttpcmstr.pc_expire
                                         and pc_mstr.pc_list_classification = 1
                                         no-lock) 
                                         then 
                                         assign v_newpirce = yes.
                                         else 
                                         assign v_newpirce = no.
 
        /* change expired date */                                 
   if can-find (first pc_mstr where pc_mstr.pc_domain = global_domain
                         and pc_mstr.pc_list = ttpcmstr.pc_list
                                         and pc_mstr.pc_part = ttpcmstr.pc_part
                                         and pc_mstr.pc_um   = ttpcmstr.pc_um
                                         and pc_mstr.pc_amt_type = ttpcmstr.pc_amt_type
                                         and pc_mstr.pc_start < ttpcmstr.pc_start 
                                         and pc_mstr.pc_expire >= ttpcmstr.pc_start
                                         and   pc_mstr.pc_list_classification  = 1
                                         no-lock) 
      
                                         then assign v_existpc = yes.
                         else v_existpc = no.
        
         put stream f_cim unformatted v_quote ttpcmstr.pc_list v_quote v_space   /* Price list */
                                  v_quote ttpcmstr.pc_desc v_quote v_space   /* decription */
                                  v_default v_space                          /* product line */
                                  v_quote ttpcmstr.pc_curr v_quote v_space   /* currency */
                                                                  v_quote ttpcmstr.pc_part v_quote v_space   /* item number */
                                  v_quote ttpcmstr.pc_um   v_quote v_space   /* unit of measure */
                                  v_quote ttpcmstr.pc_start v_quote  skip.   /*  start date */
     
         if v_existpc then
         put stream f_cim unformatted v_quote "Y" v_quote v_space skip   .             /* Change last expired date */
         put stream f_cim unformatted v_quote ttpcmstr.pc_expire v_quote v_space .  /* expired date */
         if can-find(poc_ctrl where poc_domain = global_domain 
                                and poc_temp_price_enabled = yes ) then
   
   /*****put stream f_cim unformatted v_default v_space .                           /* temporary price */
         delete by shirly 2021-03-03*******/
         
         /** add by shirly 2021-03-03 temp-price**/
         put stream f_cim unformatted v_quote ttpcmstr.pc_temp_remark v_quote  v_space .

         put stream f_cim unformatted v_quote ttpcmstr.pc_amt_type v_quote v_space skip. /* amount type */                        
         find first pc_mstr where pc_domain   = global_domain
                            and   pc_list     = ttpcmstr.pc_list
                            and   pc_start    = ttpcmstr.pc_start
                            and   pc_expire   = ttpcmstr.pc_expire
                            and   pc_curr     = ttpcmstr.pc_curr
                            and   pc_amt_type = ttpcmstr.pc_amt_type
                            and   pc_part     = ttpcmstr.pc_part
                            and   pc_um       = ttpcmstr.pc_um
                            and   pc_mstr.pc_list_classification = 1
                            and   pc_temp_pl = yes
                            no-lock no-error.
         if avail pc_mstr then put stream f_cim unformatted "YES" skip.                   


         /*add by shirly 2021-03-03�����ʱ�۸�=yes�����ڼ۸�ά��ǰ���һ��Ļ  ���棺�۸�����Ϊ��ʱ�۸񣬼�����YES*/
         if ttpcmstr.pc_temp_remark = "YES" then do:
            put stream f_cim unformatted v_quote ttpcmstr.pc_temp_remark v_quote v_space skip.   /*if temp-price : yes */
         end.
                                    
         put stream f_cim unformatted v_default v_space
                                      v_quote ttpcmstr.pc_amt[1] v_quote v_space skip. /* price   */                
    /* GY02 REMOVED
    /* GY01 ADD BEGIN */
      find first ttcost where ttcost.tt_part = ttpcmstr.pc_part
                        and ttcost.tt_pricelist = ttpcmstr.pc_list
                                        and ttcost.tt_taskid    = ttpcmstr.pc_taskid
                                        no-error.
          find sct_det where sct_det.sct_domain = global_domain
                        and sct_det.sct_part = ttpcmstr.pc_part
                                        and sct_det.sct_site = v_site
                                        and sct_det.sct_sim = "standard"
                                        and sct_det.sct_mtl_tl = ttpcmstr.pc_amt[1] no-lock no-error.
         
      if not available ttcost and not available sct_det 
          then do:
          create ttcost.
          assign ttcost.tt_part = ttpcmstr.pc_part
                 ttcost.tt_pricelist = ttpcmstr.pc_list
                         ttcost.tt_cost      = ttpcmstr.pc_amt[1]
                         ttcost.tt_site      = if substring(ttpcmstr.pc_list,length(ttpcmstr.pc_list) - 1,2) = "J1" then "BJ01"
                                               else if substring(ttpcmstr.pc_list,length(ttpcmstr.pc_list) - 1,2) = "J2" then "BJ02"
        else if substring(ttpcmstr.pc_list,length(ttpcmstr.pc_list) - 1,2) = "J3" then "BJ03"

                                                                   else "ERROR"
                         ttcost.tt_startdate = ttpcmstr.pc_start
                         ttcost.tt_taskid    = ttpcmstr.pc_taskid
                 ttcost.tt_error    = ttcost.tt_site.
          
          end.

    /* GY01 ADD END   */
    */        
         
         if last-of(ttpcmstr.pc_taskid) then do:

         output stream f_cim close.
            v_err = no.
                v_errstring = "".
         cimloop:
     do transaction on error undo cimloop, leave cimloop:         
            batchrun = yes.
        output to value(v_log).
        input from value(v_cim).
        {us/bbi/gprun.i ""ppvnpcmt.p""}
        input close.
        output close.
        batchrun = no.                

                
                FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size > 0 then
                do:
                input from value(v_log).              
          repeat:
          import unformatted v_fline.
                  if index(v_fline, getTermLabel("Error",5)) <> 0 or 
                     index(v_fline, "**") <> 0  then
                     do:
                                         assign 
                         v_err    = yes
                         v_errstring = v_fline + "|" + v_errstring.
                       leave.
                   end. /* end if index */                      
           end. /* repeat */               
        input close. /* input from value(v_log). */        
        end. /* if exist log file */                
                else do:
                v_err = yes.
                v_errstring = "LOG FILE NOT EXIST".
                end.
                
                /* check if all price list loaded into QAD */
                for each b_ttpcmstr where b_ttpcmstr.pc_taskid = ttpcmstr.pc_taskid
                                      and b_ttpcmstr.pc_domain = global_domain
                                                          .
                        find pc_mstr where pc_mstr.pc_domain = global_domain
                                       and pc_mstr.pc_list = b_ttpcmstr.pc_list
                                                   and pc_mstr.pc_part = b_ttpcmstr.pc_part
                                                   and pc_mstr.pc_start = b_ttpcmstr.pc_start
                                                   and pc_mstr.pc_expire = b_ttpcmstr.pc_expire
                                                   and pc_mstr.pc_list_classification = 1
                                                   and pc_mstr.pc_amt_type = b_ttpcmstr.pc_amt_type
                                                   and pc_mstr.pc_amt[1] = b_ttpcmstr.pc_amt[1]
                                                   no-lock no-error.
                        if not available pc_mstr then do:
                    v_err = yes.
                    v_errstring = v_errstring + "|" + "PC Master no exist for " + b_ttpcmstr.pc_part + b_ttpcmstr.pc_list.                
                    end.
                        
                end.
                
                if v_err then
        do:
               /*   message  " Errors found please check!". */
                  undo cimloop , leave cimloop.                     
        end.                 
         end. /* do transaction on error undo cimloop, leave cimloop:        */
         
         
         
                 /* Update Task status */
                find first tea_task_sub where tea_task_sub.taskid = ttpcmstr.pc_taskid
                              and upper(tea_task_sub.tablename) = "TES_PURCHASE_PRICE"
                                                  and upper(tea_task_sub.domain)    = upper(global_domain)
                                                  and upper(tea_task_sub.subscriber) = "QAD"
                              no-error.
            if available tea_task_sub then
             assign tea_task_sub.TaskState  = if v_err then 3 else 2
                    tea_task_sub.FailedInfo = if v_err then v_errstring else "Success"
                    tea_task_sub.UpdateUser = global_userid
                            tea_task_sub.UpdateTime = datetime(today,mtime).
                         
         if v_err then do:                 
        /* Move file from work to out */
        FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_log).
          unix silent value("mv " + v_log + ' ' +  m_outpath).
        end.
        FILE-INFO:filename = (v_cim).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_cim).
          unix silent value("mv " + v_cim + ' ' +  m_outpath).
        end.
     end.
         else do:
                 /* Move file from work to archive */
        FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_log).
          unix silent value("rm -rf " + v_log).
        end.
        FILE-INFO:filename = (v_cim).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_cim).
          unix silent value("rm -rf " + v_cim).
        end.
        
                
                /** GY02 ADD BEGIN */
                         
                                 
                                 end. /* no error else do: */
         
         
         end. /* if last-of (ttpcmstr.pc_taskid) */
         
     end. /* if not can-find */
   end. /* for each ttpcmstr */
   
                /*** for each pc_mstr where pc_mstr.pc_domain = global_domain
                                   and pc_mstr.pc_start  <= today
                                                   and pc_mstr.pc_expire >= today
                                                   no-lock break by pc_mstr.pc_part by pc_mstr.pc_mod_date.***/
              for each pc_mstr no-lock where pc_mstr.pc_domain = global_domain
                                   and pc_mstr.pc_start  <= today
                                                   and pc_mstr.pc_expire >= today ,
                                    each pt_mstr no-lock where 
                                    pt_domain = global_domain and pt_part = pc_mstr.pc_part
                                              and pt_um = pc_mstr.pc_um 
                                              break by pc_mstr.pc_part by pc_mstr.pc_mod_date:              ~                      /* add pt_um = pc_um by shirly 2024-02-28 */        
                                                                                          
              if last-of(pc_mstr.pc_part) then do:
                                                   
                find first ttcost where ttcost.tt_part = pc_mstr.pc_part
                        and ttcost.tt_pricelist = pc_mstr.pc_list                                      
                                        no-error.
          find sct_det where sct_det.sct_domain = global_domain
                        and sct_det.sct_part = pc_mstr.pc_part
                                        and sct_det.sct_site = v_site
                                        and sct_det.sct_sim = "standard"
                                        and sct_det.sct_mtl_tl = pc_mstr.pc_amt[1] no-lock no-error.
         
      if not available ttcost and not available sct_det 
          then do:
          create ttcost.
          assign ttcost.tt_part = pc_mstr.pc_part
                 ttcost.tt_pricelist = pc_mstr.pc_list
                         ttcost.tt_cost      = pc_mstr.pc_amt[1]
                         ttcost.tt_site      = if substring(pc_mstr.pc_list,length(pc_mstr.pc_list) - 1,2) = "J1" then "BJ01"
                                               else if substring(pc_mstr.pc_list,length(pc_mstr.pc_list) - 1,2) = "J2" then "BJ02"
    else if substring(pc_mstr.pc_list,length(pc_mstr.pc_list) - 1,2) = "J3" then "BJ03"
                        
                                                                   else "ERROR"
                         ttcost.tt_startdate = pc_mstr.pc_start
                      
                 ttcost.tt_error    = ttcost.tt_site.
                 
           /****add by shirly 2021-05-29 begin****/       
           if ttcost.tt_site <> "ERROR" then do:      
              find first ptp_det no-lock where ptp_domain = global_domain 
              and ptp_site = ttcost.tt_site and ptp_part =  pc_mstr.pc_part              
              no-error. 
              if avail ptp_det  then
               assign ttcost.tt_pm_code = ptp_pm_code. /*����L��*/
               else
                    assign ttcost.tt_pm_code = "".
              if available ptp_det and ptp_pm_code = "P"then do:
                 /** find first pt_mstr no-lock where pt_domain = global_domain
                 and pt_part = pc_mstr.pc_part no-error.
                 *
                 if available pt_mstr and pt_promo = "BJ02TO01" then 
                     ****/
                     if pt_promo = "BJ02TO01"  then 
                     assign ttcost.tt_site = "BJ02".
               
                 /*else if available pt_mstr and pt_promo = "BJ01TO02" then 
                     */
                   else if pt_promo = "BJ01TO02" then   
                     assign ttcost.tt_site = "BJ01"
              .                    
              end.
              else assign ttcost.tt_error = "ERROR".   /* ��ΪP�� */
           end.  
           /****add by shirly 2021-05-29 end ****/  
          end.
       end.
    end. /* for each pc_mstr where pc_mstr.pc_domain = global_domain */
          /* GY02 ADD END */
        /* GY01 ADD BEGIN*/
    
        for each ttcost no-lock:
        find first ptp_det where ptp_det.ptp_domain = global_domain
                             and ptp_det.ptp_site = ttcost.tt_site
                             and ptp_det.ptp_part = ttcost.tt_part
                             no-lock no-error.
        if avail ptp_det and (ptp_det.ptp_pm_code = "L" or                              ptp_det.ptp_pm_code = "")
        then 
            delete ttcost.
        end.  /*double check L 20210919 tianyu add*/
                
                
                
                
                

  for each ttcost where ttcost.tt_error <> "ERROR" and (ttcost.tt_pm_code <> "L"
      and ttcost.tt_pm_code <> "") /*and ttcost.tt_site = v_site*/  /*remove by tianyu */
                                      /*   and ttcost.tt_taskid = ttpcmstr.pc_taskid */ /* GY02 REMOVE */
                                        break by ttcost.tt_part.

    
     v_cim = m_workpath + "COST" + "_" + string(ttcost.tt_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
         v_log = m_workpath + "COST" + "_" + string(ttcost.tt_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
     output stream f_cim to value(v_cim). 
         
         put  stream f_cim unformatted "yes" skip
                v_quote tt_part v_quote v_space
                                v_quote tt_site v_quote    skip
                                v_quote "standard" v_quote  skip
                                v_quote "����"     v_quote v_space
                                v_quote tt_cost  v_quote skip
                                "." skip
                                "." skip
                                "." skip
                .
         output stream f_cim close.
         
         
         /* costcimloop load process */
         
         costcimloop:
     do transaction on error undo costcimloop, leave costcimloop:         
                                
        
                batchrun = yes.
        output to value(v_log).
        input from value(v_cim).
        
        {us/bbi/gprun.i ""ppcsbtld.p""}
                
        input close.
        output close.
        batchrun = no.
                
                v_costerr = no.
                v_errstring = "".
                FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size > 0 then
                do:
                input from value(v_log).              
          repeat:
          import unformatted v_fline.
                  if index(v_fline, getTermLabel("Error",5)) <> 0 or 
                     index(v_fline, "**") <> 0  then
                     do:
                                         assign 
                         v_costerr    = yes
                         v_errstring = v_fline + "|" + v_errstring.
                       leave.
                   end. /* end if index */                      
           end. /* repeat */               
        input close. /* input from value(v_log). */        
        end. /* if exist log file */                
                else do:
                v_costerr = yes.
                v_errstring = "LOG FILE NOT EXIST".
                end.
                
                /* validation transaction history  */
                

                
                if v_costerr then
        do:
                  status default " Errors found please check!" + v_errstring + ttcost.tt_part.
                                  ttcost.tt_error = v_errstring.
                  undo costcimloop , leave costcimloop.                     
        end. 
                
         end. /* do transaction on error undo costcimloop, leave costcimloop:        */
         
          
         if v_costerr then do:                 
        /* Move file from work to out */
        FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_log).
          unix silent value("mv " + v_log + ' ' +  m_outpath).
        end.
        FILE-INFO:filename = (v_cim).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_cim).
          unix silent value("mv " + v_cim + ' ' +  m_outpath).
        end.
     end.
         else do:
                 /* Move file from work to archive */
        FILE-INFO:filename = (v_log).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_log).
          unix silent value("rm -rf " + v_log).
         /** unix silent value("mv " + v_cim + ' ' +  m_archpath ).
        **/
        end.
        FILE-INFO:filename = (v_cim).
        if FILE-INFO:file-size >= 0 then
        do:
          unix silent value("chmod 766 " + v_cim).
          unix silent value("rm -rf " + v_cim).
          /*
          unix silent value("mv " + v_log + ' ' +  m_archpath).
        */
        end.
         end.
         


         
   end. /* for each tt_cost */

    /* GY01 ADD END */          
                
/* GY02 REMOVED */                
/*          end. /* no error else do: */
         
         
         end. /* if last-of (ttpcmstr.pc_taskid) */
         
     end. /* if not can-find */
   end. /* for each ttpcmstr */ */
 
 
   end. /* if v_import */
   


   
   
     /* PRINTER SELECTION */
  {us/gp/gpselout.i &printType = "printer"
   &printWidth = 132
   &pagedFlag = " "
   &stream = " "
   &appendToFile = " "
   &streamedOutputToTerminal = " "
   &withBatchOption = "yes"
   &displayStatementType = 1
   &withCancelMessage = "yes"
   &pageBottomMargin = 6
   &withEmail = "yes"
   &withWinprint = "yes"
   &defineVariables = "yes"}
   
   
   for each ttpcmstr.
   
   put    unformatted
          ttpcmstr.pc_list       ","
          ttpcmstr.pc_desc   ","
          ttpcmstr.pc_part      ","
          ttpcmstr.pc_supplier      ","
                  ttpcmstr.pc_taskid      ","
          ttpcmstr.pc_domain    ","
          ttpcmstr.pc_um      ","
                  ttpcmstr.pc_amt_type      ","
                  ttpcmstr.pc_amt[1]     ","
                  ttpcmstr.pc_start     ","
                  ttpcmstr.pc_expire     ","
                  ttpcmstr.pc_error 
                  
 
                skip.                  
                                                                                  
   end.
   
   for each ttcost.
   
   export
   ttcost.
   
   end.
   

   
  {us/mf/mfrtrail.i}
end. /* repeat on error undo mainloop, retry mainloop: */
